<?php
defined('BASEPATH') OR exit('No direct script access allowed');
/***
	报价项目编号：新建、按年搜索 & 列表 & 删除一体化。
	
**/
class Bid_project_code extends CI_Controller {
	
	public function __construct() {
		parent::__construct();
		$this->load->library('session');
		$this->load->model('bid_project_code_model');
		$this->load->model('user_model');
		$this->load->helper('url');
	}
	//综合处理
	public function index($year = 0, $id = 0, $del = 0, $code = NULL) {
		isset($_SESSION['user_name']) OR exit('请先登录！');	
		$user_id = $_SESSION['user_id'];
		
		$data['del_info'] = "无删除信息。";
		//新建表单的提交
		if (isset($_POST['form1'])) {
			$project_code = $_POST['project_code'];
			$year = substr($project_code,2,2);
			$this->bid_project_code_model->create();
		}
		
		//查询表单的提交
		if (isset($_POST['form2'])) {
			$year = $_POST['year'];
		}
		
		//修改表单的提交
		if (isset($_POST['form3'])) {
			$project_code = $_POST['project_code'];
			$project_id = $_POST['id'];
			$year = substr($project_code,2,2);
			$this->bid_project_code_model->update_by_id($project_id);
		}
		
		//来自删除链接
		if ($del <> 0) {
			$year = substr($code,2,2);
			
			$this->bid_project_code_model->delete_by_id ($id);
			$data['del_info'] = "删除成功。";
		}
		
		//来自修改的链接
		if ($id <> 0 and $del == 0) {
			$year = substr($code,2,2);
		}
		
		$data['title'] = "报价编号列表";
		$this->load->library('parser');
		
		$this->load->view('templates/head');
		$this->load->view('templates/menu');
		if ($user_id == 'sunjingyi' || $user_id == 'likunhui') {
		if ($id <> 0 and $del == 0) {
			$my_values = $this->bid_project_code_model->get_by_id($id);
			$data['project_code'] = $my_values['code'];
			$data['project_name'] = $my_values['name'];
			$data['client'] = $my_values['client'];
			$data['equipment'] = $my_values['equipment'];
			$data['status'] = $my_values['status'];
			$data['remark'] = $my_values['remark'];
			$data['id'] = $my_values['id'];
	
			$this->parser->parse('bid_project_code/edit', $data);
		} else {
			$this->parser->parse('bid_project_code/create', $data);
		}
		}
		$this->parser->parse('bid_project_code/search', $data);
		
		//只要year不为0，就去搜编号；
		//这样新建编号后，就可以立即去查询，看看结果怎么样
		if ($year <> 0 ) {
			$data['project_codes']=$this->bid_project_code_model->get_by_year($year);
			//DC list
			if ($user_id == 'sunjingyi' || $user_id == 'likunhui') {
				$this->parser->parse('bid_project_code/list_a', $data);
			} else {
			//Others list
				$this->parser->parse('bid_project_code/list', $data);
			}
		}
				
		
		//所选客户联系人列表
		$this->load->view('templates/foot');
	}
	
}
